package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;


import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    /**
     * 查询所有的品牌
     */
    @Override
    public List<Brand> findAll() {
        List<Brand> brands = brandMapper.selectAll();
        for (Brand brand : brands) {
            System.out.println(brand);
        }
        return brands;
    }

    /**
     * 根据id查询品牌
     *
     * @param id
     */
    @Override
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    /**
     * 新增品牌
     *
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 修改品牌数据
     *
     * @param brand
     */
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 删除品牌数据
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 多条件查询
     *
     * @param brand
     */
    @Override
    public List<Brand> findList(Brand brand) {
        Example example = createExample(brand);
        List<Brand> brands = brandMapper.selectByExample(example);
         return brands;

    }

    /**
     * 分页查询
     *
     * @param page 当前页数
     * @param size 每页显示多少行
     */
    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size) {
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Brand>(brandMapper.selectAll());
    }

    /**
     * 条件分页查询
     *
     * @param brand
     * @param page
     * @param size
     */
    @Override
    public PageInfo<Brand> findPageAndList(Brand brand, Integer page, Integer size) {
        //用于分页查询
        PageHelper.startPage(page,size);
        //条件查询
        Example example = createExample(brand);
        //执行搜索条件
        return  new PageInfo<Brand>(brandMapper.selectByExample(example));

    }


    /**
     * 用于条件查询的公共类
     */
    public Example createExample(Brand brand){
        //构建条件查询
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(brand!=null){
            //品牌名称
            if(!StringUtils.isEmpty(brand.getName())){
                criteria.andLike("name","%"+brand.getName()+"%");
            }
            //品牌图片地址
            if(!StringUtils.isEmpty(brand.getImage())){
                criteria.andLike("image","%"+brand.getImage()+"%");
            }
            //品牌的首字母
            if(!StringUtils.isEmpty(brand.getLetter())){
                criteria.andLike("letter","%"+brand.getLetter()+"%");
            }
            //品牌id
            if(!StringUtils.isEmpty(brand.getId())){
                criteria.andLike("id","%"+brand.getId()+"%");
            }
            //排序
            if(!StringUtils.isEmpty(brand.getSeq())){
                criteria.andLike("seq","%"+brand.getSeq()+"%");
            }
        }
        return example;
    }
}
